﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.DAL.DAOs;

namespace LibrarySystem.Controllers.LibraryControllers
{
    public class SkippingDetailController : Controller
    {
        [HttpPost]
        public JsonResult GetSkippingDetailList(int skippingId = 0, int jStartIndex = 0, int jPageSize = 0, string jSorting = null)
        {
            try
            {
                Library_SkippingDetails libSkipDetails = new Library_SkippingDetails();
                List<SkippingDetailModel> skipDetailList = libSkipDetails.GetDataList(skippingId, jStartIndex, jPageSize, jSorting);
                return Json(new { Result = "OK", Records = skipDetailList, TotalRecordCount = skipDetailList.Count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult AddSkippingDetail(SkippingDetailModel model)
        {
            try
            {
                Library_SkippingDetails libSkipDetails = new Library_SkippingDetails();
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                var add = libSkipDetails.AddNewData(model);
                return Json(new { Result = "OK", Record = add });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult UpdateSkippingDetail(SkippingDetailModel model)
        {
            try
            {
                Library_SkippingDetails libSkipDetails = new Library_SkippingDetails();
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libSkipDetails.UpdateData(model);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult DeleteSkippingDetail(int Id)
        {
            try
            {
                Library_SkippingDetails libSkipDetails = new Library_SkippingDetails();
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libSkipDetails.DeleteData(Id);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

    }
}
